package org.vectrics.mail;


import org.apache.log4j.*;
import java.util.*;
import javax.mail.*;


public class Mailer extends MailSender {

    private static Category log = Category.getInstance(Mailer.class);
    private MailerConfig config = new MailerConfig();


    public Mailer() {
    }


    synchronized protected Session getMailSession() {
        Session session = null;

        if ((config.getSmtpHost() == null) || (config.getSmtpHost().trim().length() == 0)) {
            log.error("getMailSession() - SMTP host is not configured.");
        }
        else {
            Properties props = new Properties();
            props.put("mail.smtp.host", config.getSmtpHost());
            // fill props with any information

             // Setup password authentication
              final PasswordAuthentication pa = new PasswordAuthentication(config.getUser(), config.getPassword());
              Authenticator a = new Authenticator()
              {
                 protected PasswordAuthentication getPasswordAuthentication()
                 {
                    return pa;
                 }
              };


            session = Session.getInstance(props, a);
        }
        return(session);
    }

    
    public MailerConfig getConfig() {
        return(config);
    }


    public boolean setConfig(MailerConfig config) {
        boolean success = false;

        if (config == null) {
            log.error("start() - NULL config cannot be passed to this method.");
        }
        else {
            this.config = config;
        }
        return(success);
    }


}